<?php
/**
 * Session Helper provides access to the Session in the Views.
 *
 */

/**
 * Session Helper.
 *
 */
class sessionHelper {


	/**
 * True if the Session is still valid
 *
 * @var bool
 */
	public static $valid = false;

/**
 * Error messages for this session
 *
 * @var array
 */
	public static $error = false;

/**
 * User agent string
 *
 * @var string
 */
	protected static $_userAgent = '';

/**
 * Path to where the session is active.
 *
 * @var string
 */
	public static $path = '/';

/**
 * Error number of last occurred error
 *
 * @var int
 */
	public static $lastError = null;

/**
 * Start time for this session.
 *
 * @var int
 */
	public static $time = false;

/**
 * Cookie lifetime
 *
 * @var int
 */
	public static $cookieLifeTime;

/**
 * Time when this session becomes invalid.
 *
 * @var int
 */
	public static $sessionTime = false;

/**
 * Current Session id
 *
 * @var string
 */
	public static $id = null;

/**
 * Hostname
 *
 * @var string
 */
	public static $host = null;

/**
 * Session timeout multiplier factor
 *
 * @var int
 */
	public static $timeout = null;

/**
 * Number of requests that can occur during a session time without the session being renewed.
 * This feature is only used when config value `Session.autoRegenerate` is set to true.
 *
 * @var int
 * @see CakeSession::_checkValid()
 */
	public static $requestCountdown = 10;

	public static $arr  = array();

/**
 * Whether or not the init function in this class was already called
 *
 * @var bool
 */
	protected static $_initialized = false;

/**
 * Session cookie name
 *
 * @var string
 */
	protected static $_cookieName = null;

	protected static $sessionconfig  = null;



/**
 * Used to check is a session key has been set
 *
 * In your view: `$this->Session->check('Controller.sessKey');`
 *html#SessionHelper::check
 */
	public function check($name) {
		if(empty($name) || $name == null ){
			return null;
		}
		if(isset($_SESSION[$name])){
			return true;
		}
		return false;
	}

/**
 * Returns last error encountered in a session
 *
 * 	In your view: `$this->Session->error();`
 *	html#displaying-notifications-or-flash-messages
 */
	
	public static function error() {
		if (self::$lastError) {
			return self::_error(self::$lastError);
		}
		return false;
	}

	protected static function _error($errorNumber) {
		if (!is_array(self::$error) || !array_key_exists($errorNumber, self::$error)) {
			return false;
		}
		return self::$error[$errorNumber];
	}

	public function selflash($name){
		$_SESSION['flash'] = $name;
	}

	public function getflash(){
		$value =  $_SESSION['flash'];
		unset($_SESSION['flash']);
		return $value;
	}

	public  function write($names , $values = null){
		
		
		if (session_status() == PHP_SESSION_NONE) {
		    session_start();
		}

		if(is_array($names)){
			
			$result = 	self::sessionwrite($names);
					
			foreach ($names as $key => $name) {
					
				$_SESSION[$key] = $result;
				break;
				# code...
			}

			return $result;
		}else{
			if (empty($names) ||  $names == null) {
				return null;
			}

			$_SESSION[$names] = $values;
		}
		
		

		return $result;
	
	}
	public function sessionwrite($names ,$seskey =array() , $count = 1){
			

		foreach ($names as $key => $name) {
			self::$arr[] = $key;
			
			if(is_array($name)){
				
				$seskey = array();
				$seskey[] = $name;
				
				self::sessionwrite($name ,$seskey , $count + 1);
			}
			$seskey = array();
			$seskey[] = $name;
		} 

		return $seskey;
	}


	protected static function _setError($errorNumber, $errorMessage) {
		if (self::$error === false) {
			self::$error = array();
		}
		self::$error[$errorNumber] = $errorMessage;
		self::$lastError = $errorNumber;
	}

	public  function read($name = null) {
		if (empty($name) && $name !== null) {
			return false;
		}
		if (session_status() == PHP_SESSION_NONE) {
		    return false;
		}
		
		$result = self::sessionread($_SESSION,$name);
		


		

		if (isset($result)) {
			return $result;
		}
		return null;
	}

	public  static function sessionread($SESSION,$name){
		if (session_status() == PHP_SESSION_NONE) {
		    session_start();
		}
		
		foreach ($SESSION as $key => $value) {
			
			if($key == $name){
				return $SESSION[$key];
			}
			
			if(is_array($value)){
				
				self::sessionread($value,$name);
			}
			
		}
		
		return false;
	}

	protected static function _hasSession() {
		return self::started();
	}

	protected static function _startSession() {
		self::init();
		session_write_close();
		self::_configureSession();

		if (headers_sent()) {
			if (empty($_SESSION)) {
				$_SESSION = array();
			}
		} else {
			// For IE<=8
			session_cache_limiter("must-revalidate");
			session_start();
		}
		return true;
	}

	protected static function _defaultConfig($name) {
		$defaults = array(
			'php' => array(
				'cookie' => 'eduperk',
				'timeout' => 240,
				'ini' => array(
					'session.use_trans_sid' => 0,
					'session.cookie_path' => self::$path
				)
			),
			'cake' => array(
				'cookie' => 'CAKEPHP',
				'timeout' => 240,
				'ini' => array(
					'session.use_trans_sid' => 0,
					'url_rewriter.tags' => '',
					'session.serialize_handler' => 'php',
					'session.use_cookies' => 1,
					'session.cookie_path' => self::$path,
					'session.save_path' => TMP . 'sessions',
					'session.save_handler' => 'files'
				)
			),
			'cache' => array(
				'cookie' => 'CAKEPHP',
				'timeout' => 240,
				'ini' => array(
					'session.use_trans_sid' => 0,
					'url_rewriter.tags' => '',
					'session.use_cookies' => 1,
					'session.cookie_path' => self::$path,
					'session.save_handler' => 'user',
				),
				'handler' => array(
					'engine' => 'CacheSession',
					'config' => 'default'
				)
			),
			'database' => array(
				'cookie' => 'CAKEPHP',
				'timeout' => 240,
				'ini' => array(
					'session.use_trans_sid' => 0,
					'url_rewriter.tags' => '',
					'session.use_cookies' => 1,
					'session.cookie_path' => self::$path,
					'session.save_handler' => 'user',
					'session.serialize_handler' => 'php',
				),
				'handler' => array(
					'engine' => 'DatabaseSession',
					'model' => 'Session'
				)
			)
		);
		if (isset($defaults[$name])) {
			return $defaults[$name];
		}
		return false;
	}

	

	protected static function _cookieName() {
		if (self::$_cookieName !== null) {
			return self::$_cookieName;
		}

		self::init();
		self::_configureSession();

		return self::$_cookieName = session_name();
	}

	public static function clear() {
		$_SESSION = null;
		self::$id = null;
		self::renew();
	}

	public static function destroy($name){
		if($name==null || $name == ""){
			session_destroy();
			$_SESSION = null;
			return null;
		}
		unset($_SESSION[$name]);
		return null;
	}

}